var bmap = require('../../libs/bmap-wx.min.js');
var wxMarkerData = [];
Page({
    data: {
        markers: [],
        latitude: '',
        longitude: '',
        sugData:'',
        placeData: {},
  },
  bindKeyInput: function (e) {
    var that = this;
    if (e.detail.value === '') {
      that.setData({
        sugData: ''
      });
      return;
    }else{
      that.setData({
        sugData: e.detail.value
      });
      that.searchAction(that.data.sugData);
    }
  },
    makertap: function(e) {
      
        var that = this;
        var id = e.markerId;
        that.showSearchInfo(wxMarkerData, id);
        that.changeMarkerColor(wxMarkerData, id);
    },
    onLoad: function() {
        var that = this;
      
        that.searchAction(that.data.sugData)
    },
    searchAction:function(des){
      var that = this;
      var app = getApp();
      var BMap = new bmap.BMapWX({
        ak: app.globalData.bMapAK
      });

      var fail = function (data) {
        console.log(data)
      };
      var success = function (data) {
        wxMarkerData = data.wxMarkerData;
        that.setData({
          markers: wxMarkerData
        });
        that.setData({
          latitude: wxMarkerData[0].latitude
        });
        that.setData({
          longitude: wxMarkerData[0].longitude
        });
      }
      console.log('fail!!!!' + that.data.sugData);
      BMap.search({
        "query": des,   ///'美食'
        fail: fail,
        success: success,
        iconPath: '../../img/marker_red.png',
        iconTapPath: '../../img/marker_red.png'
      });
    },
    showSearchInfo: function(data, i) {
        var that = this;
        that.setData({
            placeData: {
                title: '名称：' + data[i].title + '\n',
                address: '地址：' + data[i].address + '\n',
                telephone: '电话：' + data[i].telephone
            }
        });
    },
    changeMarkerColor: function(data, id) {
        var that = this;
        var markersTemp = [];
        for (var i = 0; i < data.length; i++) {
            if (i === id) {
                data[i].iconPath = "../../img/marker_yellow.png";
            } else {
                data[i].iconPath = "../../img/marker_red.png";
            }
            markersTemp[i] = data[i];
        }
        that.setData({
            markers: markersTemp
        });
    }
})